<template>
  <!--勘察单位新增弹层start -->
  <el-dialog
    v-dialogDrag
    :title="dialogTitle"
    :close-on-click-modal="false"
    :visible.sync="dialogVisible"
    class="dialogStyleMiddle"
    :before-close="cancel"
  >
    <el-form ref="formData" :model="formData" :rules="rules" label-width="80px">
      <el-row>
        <el-col>
          <el-form-item prop="date" label="巡查时间">
            <el-date-picker
              v-model="formData.date"
              type="daterange"
              size="small"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="handleChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="" prop="remark">
            <label slot="label" v-html="`备&emsp;&emsp;注`" />
            <el-input v-model="formData.remark" style="width:100%" :maxlength="500" :rows="3" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-divider />
    <el-form :inline="true">
      <el-form-item label="勘察单位">
        <el-input
          v-model="filters.name"
          size="medium"
          class="serach-person"
          placeholder="请输入勘察单位名称"
        />
        <el-button size="small" type="primary" @click="getList">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="multipleTable"
      :data="tableData"
      style="width: 100%;"
      stripe
      class="table-list"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="企业名称" min-width="120" align="center" />
      <el-table-column prop="province" :formatter="(row) => getRegionCode(row.province, row.city, row.area)" :show-overflow-tooltip="true" label="所在地区" width="180" align="center" />
      <el-table-column prop="legalpersonMobile" :show-overflow-tooltip="true" label="联系电话" width="150" align="center" />
      <!--<el-table-column prop="sysEnterpriseType" label="企业类型" width="180" align="center">
        <template slot-scope="{row}">
          <span v-if="row.sysEnterpriseType === '04'">勘察单位</span>
        </template>
      </el-table-column>
      <el-table-column prop="creditCodePic" :show-overflow-tooltip="true" label="信用代码" width="180" align="center" />-->
    </el-table>
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      :page-sizes="[8,15,20,50,100]"
      :page-count="filters.pageCount"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <DialogFooter />
    </div>
  </el-dialog>
</template>

<script>
import { listRegionTree } from '@/api/common/area.js'
import { addInspectoUnit } from '@/api/projectManage/projectData'
import { listSurveyUnit } from '@/api/projectManage/projectData'
import DialogFooter from '@/components/Common/Btns/DialogFooter/index'
export default {
  components: {
    DialogFooter
  },
  data() {
    return {
      // 选择的单位
      multipleSelection: [],
      rules: {
        date: [{ required: true, message: '巡查开始和结束时间不能为空!', trigger: 'change' }],
        remark: [{ required: true, message: '备注不能为空!', trigger: 'blur' }]
      },
      formData: {
        date: '',
        remark: '',
        endTime: '',
        startTime: ''
      },
      // 筛选勘察单位
      filters: {
        pageNum: 1,
        pageSize: 8,
        pageCount: 5,
        name: '',
        sysEnterpriseType: '04'
      },
      // 勘察单位列表
      tableData: [],
      proId: '',
      total: 0,
      /* 新增 start*/
      dialogTitle: '', // 弹层标题
      dialogVisible: false, // 弹层是否显示
      btnDisabled: false, // 确定按钮
      // 表单验证规则
      cityOptions: [] // 地区
    }
  },
  mounted() {
    this.getRegionTree()
  },
  methods: {
    // 获取区域树
    async getRegionTree() {
      const { data } = await listRegionTree({ regionCode: '' })
      if (data) this.cityOptions = data
    },
    // 回显地区
    getRegionCode(province, city, county) {
      if (!county) return
      if (this.cityOptions.length > 0) {
        const sheng = this.cityOptions.find(sheng => sheng.value === province)
        const shi = sheng.children.find(shi => shi.value === city)
        const xian = shi.children.find(xian => xian.value === county)
        const currentProvince = (sheng.label + shi.label + xian.label) || ''
        return currentProvince
      }
    },
    /* 新增/编辑弹层 start*/
    // 新增/编辑
    handleAdd(title, proId) {
      this.dialogTitle = title
      this.dialogVisible = true
      this.proId = proId
      this.getList()
    },
    // 筛选人员
    getList() {
      listSurveyUnit(this.filters).then(res => {
        const { data: { rows, total }} = res
        this.tableData = rows
        this.total = total
      })
    },
    // 添加企业
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false
      this.btnDisabled = false
      this.emptyAddForm()
    },
    // 时间范围变化
    handleChange(value) {
      if (!value) {
        this.formData.startTime = ''
        this.formData.endTime = ''
        return
      }
      this.formData.startTime = value[0]
      this.formData.endTime = value[1]
    },
    // 提交按钮
    async submitForm() {
      this.$refs['formData'].validate(async(valid) => {
        if (valid) {
          const { multipleSelection, proId } = this
          const { startTime, endTime, remark } = this.formData
          if (multipleSelection.length) {
            const newArr = multipleSelection.map(item => ({ nodeId: item.id, proId, startTime, endTime, remark }))
            await addInspectoUnit(newArr)
            this.msgSuccess('分配成功!')
            this.cancel()
          } else {
            this.msgWarning('请选择勘察单位')
          }
        }
      })
    },
    // 点击行时,选框选中
    rowChecked(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    // 表单重置
    emptyAddForm() {
      this.resetForm('formData')
      this.multipleSelection = []
    }
    /* 新增弹层 end*/
  }
}
</script>

<style scoped>
  .el-divider--horizontal{
    margin: 0px 0 15px;
  }
</style>
